package gu.simplemq.redis;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.google.common.net.HostAndPort;

import gu.simplemq.BaseMessageQueueFactory;
import gu.simplemq.IConsumer;
import gu.simplemq.IProducer;
import gu.simplemq.IPublisher;
import gu.simplemq.ISubscriber;
import gu.simplemq.MessageQueueType;
import gu.simplemq.redis.JedisPoolLazy.PropName;

import static gu.simplemq.redis.JedisUtils.initAsRedisParameters;
import static gu.simplemq.redis.JedisPoolLazy.createInstance;
import static com.google.common.base.MoreObjects.firstNonNull;

/**
 * {@link IMessageQueueFactory}接口的redis实现类
 * @author guyadong
 *
 */
public final class MessageQueueFactoryImpl extends BaseMessageQueueFactory<JedisPoolLazy> {

	private HashMap<PropName, Object> props;
	public MessageQueueFactoryImpl() {
	}
	
	@Override
	protected void doInit(Map<String, Object> properties){
		mqConnParams = properties;
		props = initAsRedisParameters(firstNonNull(properties, Collections.<String, Object>emptyMap()));
		pool = createInstance(props);
	}
	@Override
	protected HostAndPort doGetHostAndPort() {
		return JedisUtils.getHostAndPort(props);
	}

	@Override
	public ISubscriber getSubscriber() {
		return RedisFactory.getSubscriber(getPool());
	}

	@Override
	public IPublisher getPublisher() {
		return RedisFactory.getPublisher(getPool());
	}

	@Override
	public IProducer getProducer() {
		return RedisFactory.getProducer(getPool());
	}

	@Override
	public IConsumer getConsumer() {
		return RedisFactory.getConsumer(getPool());
	}

	@Override
	public final MessageQueueType getImplType(){
		return MessageQueueType.REDIS;
	}
	@Override
	public boolean testConnect() {
		return JedisUtils.testConnect(props);
	}
}
